﻿namespace LinFu.AOP.Interfaces
{
    using System.Collections.Generic;
    using System.Linq;

    public static class MethodReplacementRegistry
    {
        #region Fields

        private static readonly List<IMethodReplacementProvider> _providers = new List<IMethodReplacementProvider>();

        #endregion Fields

        #region Properties

        public static IList<IMethodReplacementProvider> Providers
        {
            get { return _providers; }
        }

        #endregion Properties

        #region Methods

        public static IMethodReplacementProvider GetProvider(IInvocationContext context)
        {
            var matches = from p in _providers
                          where p.CanReplace(context)
                          select p;

            var resultList = matches.ToList();

            if (resultList.Count == 0)
                return null;

            return resultList.First();
        }

        #endregion Methods
    }
}